var api = require("../../api.js"),
  r = "",
  app = getApp();
import {
  $Login
} from '../../components/base.js'
Page({
  data: {
    swiperH: "",
    mineData: {},
    historiesListData: [],
    mineCommunitiesData: [],
    mine_consults: [],
    orderType: '0',
    mine_orderData: [],
    collectsData: [],
    /** 人脉动态是数据 */
    dynamic: [],
    share_data: {},
    /** 分享赚数据 */
    pageIndex: 1,
    currentTab: 0, //预设当前项的值
    currentIndex: 0,
    nowIdx: 0, //当前swiper索引
    imgList: [], //轮播组件
    indexArr: [],
    tradeData: [],
    articles: [],
    classRoom: [],
    windowHeight: 0, //视图高度
    page_index: 1,
    /** 设置为50个数据展示 暂时不考虑翻页了 */
    page_size: 50,
    loading: false,
    isBottom: false,
    height: "0px",
  },
  onLoad: function(options) {
    this.setData({
      height: wx.getSystemInfoSync().windowHeight,
    })
  },
  onShow: function() {
    r = this;
    app.getWxUserInfo().then(res => {
      //初始化数据
      this.initData();
      $Login()
    }).catch(error => {

    })
  },
  toCommunities: function(e) {
    const communitiesid = e.currentTarget.dataset.communitiesid
    wx.navigateTo({
      url: `../community/community?communitiesId=${communitiesid}`
    })
  },
  toTypeDetails: function(e) {
    let type = e.currentTarget.dataset.typedetails;
    let id = e.currentTarget.dataset.typedetailsid;
    if (type == 0) {
      wx.navigateTo({
        url: `../art/art?id=${id}`
      })
    } else if (type == 1) {
      wx.navigateTo({
        url: `../activedetial/activedetial?id=${id}`
      })
    } else if (type == 2) {
      wx.navigateTo({
        url: `/pages/productDetail/productDetail?id=${id}`
      })
    }
  },
  //主要是绑定手机号
  createMyCommunity: function() {
    if (!this.data.mineData.phone) {
      wx.showModal({
        title: '提示',
        content: '请先绑定手机号',
        success: function(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: "../login/login"
            })
          }
        }
      })
    } else {
      // if (this.data.mineData.isBindbusiness === '0') {
        wx.navigateTo({
          url: "/pages/mine/viper/viper"
        })
      // } else {
      //   wx.navigateTo({
      //     url: `../compDataDetail/compDataDetail?compId=${this.data.mineData.compId}`
      //   })
      // }
    }
  },

  /** 初始化数据方法 */
  initData: function() {
    wx.showLoading({
      title: "数据加载中"
    });
    if (r.data.currentTab == 0) {
      this.ming_index();
    } else if (r.data.currentTab == 1) { /** 订单数据 */
      this.mine_ords();
    } else if (r.data.currentTab == 2) { /** 分享赚方法 */
      this.share_data();
    } else if(r.data.currentTab == 3){ /** 人脉动态 */
      this.contactsdynamic();
    }
  },
  /** 获取的的社群 */
  ming_index(){
    const t = new Object(),that = this;
    t.open_id = app.globalData.wxUserInfo.openId;
    /** 我的简单信息数据 */
    app.getData.mine(t).then(res => {
      if (res.code == 200) {
        var n = res.data;
        r.setData({
          phone: n.isBindphone == 1 ? true : false,
          mineData: res.data.user,
        })
        console.log('mineData',res.data)
        wx.setStorageSync('userGrade', n.userGrade);
      }
    });
    let sendData = {
      open_id: app.globalData.wxUserInfo.openId,
      page_index: 1,
      page_size: 5
    }
    /** 我的关注和创建的社群 */
    wx.request({
      url: api.index.mineComms,
      data: sendData,
      header: {
        "content-type": "application/json"
      },
      method: 'GET',
      success(res) {
        setTimeout(() => {
          console.log('社群列表数据返回 -->',res)
          that.setData({
            mineCommunitiesData: res.data.data,
            page_index: that.data.page_index + 1
          })
          wx.hideLoading()
        }, 500)
      }
    });
  },
  /** 获取我的订单 */
  mine_ords(){
    const that = this;
    const t = {
      page_index: 1,
      page_size: 5,
      open_id: app.globalData.wxUserInfo.openId,
    }
    wx.request({
      url: api.index.mineOrder,
      data: t,
      header: {
        "content-type": "application/json"
      },
      method: 'GET',
      success(res) {
        setTimeout(() => {
          console.log('订单列表数据接口返回 --> ' , res)
          that.setData({
            mine_orderData: res.data.data
          })
          wx.hideLoading()
        }, 500)
      }
    });
  },

  /** 分享赚 数据 */
  share_data: function() {
    const t = new Object();
    t.open_id = app.globalData.wxUserInfo.openId;
    console.log(t.open_id)
    wx.request({
      url: api.index.share_profits,
      data: t,
      header: {
        "content-type": "application/json"
      },
      method: 'GET',
      success(res) {
        setTimeout(() => {
          console.log('分享赚数据')
          console.log(res)
          r.setData({
            loading: false,
            share_data: res.data.data
          })
          wx.hideLoading()
        }, 500)
      }
    });
  },

  /** 人脉动态接口 */
  contactsdynamic: function(){
    const that = this;
    const t = {
      page_index: 1,
      page_size: 5,
      open_id: app.globalData.wxUserInfo.openId,
    }
    wx.request({
      url: api.index.share_data,
      data: t,
      header: {
        "content-type": "application/json"
      },
      success(res) {
        setTimeout(() => {
          console.log('人脉动态数据返回 --> ' , res)
          that.setData({
            dynamic: res.data.data
          })
          wx.hideLoading()
        }, 500)
      }
    });
  },




  /** 上拉加载数据 */
  loadMoreData: function() {
    console.log('调用方法 --  loadMoreData currentTab == ' + this.data.currentTab )
    let that = this
    if (that.data.isBottom) {
      return false;
    }
    wx.showLoading({
      title: "数据加载中"
    });
    if (this.data.currentTab == 0) {
      let sendData = {
        open_id: app.globalData.wxUserInfo.openId,
        page_index: that.data.page_index,
        page_size: 5
      }
      wx.request({
        url: api.index.mineComms,
        data: sendData,
        header: {
          "content-type": "application/json"
        },
        method: 'GET',
        success(res) {
          setTimeout(() => {
            console.log(res)
            let mineCommunitiesData = that.data.mineCommunitiesData
            if (res.data.data.length > 0 && res.data.data.length === that.data.page_size) {
              res.data.data.map((item, index) => {
                mineCommunitiesData.push(item)
              })
              that.setData({
                mineCommunitiesData: mineCommunitiesData,
                page_index: that.data.page_index + 1
              })
            } else if (res.data.data.length > 0 && res.data.data.length < that.data.page_size) {
              res.data.data.map((item, index) => {
                mineCommunitiesData.push(item)
              })
              that.setData({
                loading: true,
                isBottom: true,
                mineCommunitiesData: mineCommunitiesData,
                page_index: that.data.page_index + 1
              })
            } else {
              that.setData({
                loading: true,
                isBottom: true,
              })
            }
            wx.hideLoading()
          }, 500)
        }
      });
    } else if (this.data.currentTab == 1) {
      let sendData = {
        open_id: app.globalData.wxUserInfo.openId,
        page_index: that.data.page_index,
        page_size: 5
      }
      wx.request({
        url: api.index.mineOrder,
        data: sendData,
        header: { "content-type": "application/json" },
        method: 'GET',
        success(res) {
          setTimeout(() => {
            console.log(res)
            let mineOrderData = that.data.mine_orderData
            if (res.data.data.length > 0 && res.data.data.length === that.data.page_size) {
              res.data.data.map((item, index) => {
                mineOrderData.push(item)
              })
              that.setData({
                mine_orderData: mineOrderData,
                page_index: that.data.page_index + 1
              })
            } else if (res.data.data.length > 0 && res.data.data.length < that.data.page_size) {
              res.data.data.map((item, index) => {
                mineOrderData.push(item)
              })
              that.setData({
                loading: true,
                isBottom: true,
                mine_orderData: mineOrderData,
                page_index: that.data.page_index + 1
              })
            } else {
              that.setData({
                loading: true,
                isBottom: true,
              })
            }
            
          }, 500)
        }
      });
      wx.hideLoading()
    }else if(this.data.currentTab == 3){
      that.setData({
        loading: true,
        isBottom: true,
      })
      wx.hideLoading()
    }
  },
  /** 订单详情 */
  toOrderDetails: function(e) {
    var ordNo = e.currentTarget.dataset.ordno;
    wx.navigateTo({
      url: `../orderDetails/orderDetails?ordNo=${ordNo}`
    })
  },
  swichNav: function(e) {
    var currentTab = e.target.dataset.current;
    if (r.data.currentTab == currentTab) {
      return false;
    }
    r.setData({
      currentTab: currentTab,
      pageIndex: 1,
      scrollTop: 0,
      mineCommunitiesData: [],
      mine_orderData: [],
      orderType: '0',
      loading: false
    });
  },
  changeOrderType: function(e) {
    if (e.currentTarget.dataset.ordertype == this.data.orderType) {
      return false
    } else {
      this.setData({
        orderType: e.currentTarget.dataset.ordertype
      })
    }
    this.initData()
  },
  // 滚动切换标签样式
  switchTab: function(e) {
    if (e.detail.source == 'touch') {
      r.setData({
        currentTab: e.detail.current,
        pageIndex: 1,
        scrollTop: 0
      })
    }
    r.initData();
  },
  // 阻断冒泡
  blockBubbles: function() {},
  onShareAppMessage: function(event) {
    let value = event.target.dataset.value
    return {
      title: value,
      path: `pages/activedetial/activedetial?id=${this.data.mine_orderData.otherId}`,
      success: function(res) {},
      fail: function(res) {}
    }
  }

  // /**
  //  * 页面相关事件处理函数--监听用户下拉动作
  //  */
  // onPullDownRefresh: function() {
  //   wx.showNavigationBarLoading(), console.log("下拉刷新"), r.initData();
  //   setTimeout(function() {
  //     wx.hideNavigationBarLoading(), wx.stopPullDownRefresh();
  //   }, 1500);
  // },
  // onReachBottom: function () {
  // },
});